package cn.ld.fj.web.desk;

import cn.ld.fj.entity.DeskShop;
import cn.ld.fj.service.desk.DeskShopManager;
import cn.ld.fj.service.desk.DeskShopOrderInfoManager;
import cn.ld.fj.util.RequestParamterUtil;
import net.esoar.modules.orm.Page;
import net.esoar.modules.orm.PropertyFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zuohao on 2017/8/3.
 */

@Controller
@RequestMapping("deskShopController")
public class DeskShopController {

    @Autowired
    private DeskShopManager deskShopManager;

    @Autowired
    private DeskShopOrderInfoManager deskShopOrderInfoManager;

    private Page<DeskShop> page = new Page<DeskShop>(10);

    @RequestMapping("getDeskShopList")
    @ResponseBody
    public Object getDeskShopList(HttpServletRequest request){
        List<PropertyFilter> filters= RequestParamterUtil.getParamter(request);
        if (!page.isOrderBySetted()) {
            page.setOrderBy("id");
            page.setOrder(Page.ASC);
        }
        Page<DeskShop> deskShopPage=deskShopManager.getPage(page,filters);
        List<DeskShop> deskShopList=deskShopPage.getResult();

        List<Long> deskShopOrderInfoList=deskShopOrderInfoManager.findDeskIdByDate();

        for (DeskShop deskShop:deskShopList){
            deskShop.setIsOrder(0);
            if (deskShopOrderInfoList.contains(deskShop.getId())){
                deskShop.setIsOrder(1);
            }
        }
        return deskShopManager.getPage(page,filters);
    }

    @RequestMapping("updateDeskShopStatus")
    @ResponseBody
    public Object updateDeskShopStatus(HttpServletRequest request){
        int status=Integer.valueOf(request.getParameter("status"));
        long id=Long.valueOf(request.getParameter("id"));
        DeskShop deskShop=deskShopManager.getOne(id);
        if (deskShop.getStatus()!=status) {
            deskShop.setStatus(status);
            deskShopManager.save(deskShop);
        }
        return "success";
    }

    @RequestMapping("getDeskShopOrderInfoByDeskId")
    @ResponseBody
    public Object getDeskShopOrderInfoByDeskId(@RequestParam("deskId")Long deskShopId){
        Map<String,Object> resultMap=new HashMap<>();
        resultMap.put("deskShopOrderInfo",deskShopOrderInfoManager.getDeskShopOrderInfoByDeskId(deskShopId));
        return resultMap;
    }
}
